package question.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import question.bean.QuestionDetailBean;
import utils.MyLogUtils;
import view.MyImageLoader;

/**
 * Created by Administrator on 2017/4/25.
 * 评价付费问题主页面
 */

@ContentView(R.layout.vip_question_detail)
public class VipQuestionDetail extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.vip_question_detail_questioner_ima)
    private RoundedImageView vip_question_detail_questioner_ima;
    @ViewInject(R.id.vip_question_detail_questioner_name)
    private TextView vip_question_detail_questioner_name;
    @ViewInject(R.id.vip_question_detail_questioner_detail)
    private TextView vip_question_detail_questioner_detail;
    @ViewInject(R.id.vip_question_detail_question_content)
    private TextView vip_question_detail_question_content;
    @ViewInject(R.id.vip_question_detail_answer_ima)
    private ImageView vip_question_detail_answer_ima;
    @ViewInject(R.id.vip_question_detail_answer_content)
    private TextView vip_question_detail_answer_content;
    @ViewInject(R.id.vip_question_detail_zan)
    private TextView vip_question_detail_zan;
    @ViewInject(R.id.vip_question_detail_comment)
    private TextView vip_question_detail_comment;
    @ViewInject(R.id.vip_question_detail_evaluate)
    private TextView vip_question_detail_evaluate;
    @ViewInject(R.id.vip_question_detail_zan_layout)
    private LinearLayout vip_question_detail_zan_layout;
    @ViewInject(R.id.vip_question_detail_comment_layout)
    private LinearLayout vip_question_detail_comment_layout;

    private String dialogId;
    private String type;//问题是热门点进来的还是我的提问点进来的
    private QuestionDetailBean questionDetailBean;

    private int zan;//用于回调的赞的数量
    private boolean isPraised;//是否点赞

    private static final String TAG = "VipQuestionDetail";

    private final int GETDATA = 0;
    private final int ZAN = 1;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    switch (msg.arg1) {
                        case GETDATA:
                            Gson gson = new Gson();
                            questionDetailBean = gson.fromJson((String) msg.obj, QuestionDetailBean.class);
                            if (questionDetailBean != null) {
                                QuestionDetailBean.DetailBean detail = questionDetailBean.getDetail();
                                if (detail != null) {
                                    MyImageLoader.showImage(detail.getQuestioner_headimgurl(), vip_question_detail_questioner_ima);
                                    vip_question_detail_questioner_name.setText(detail.getQuestioner_name());
                                    vip_question_detail_questioner_detail.setText(detail.getQuestioner_city() + "/" + detail.getQuestioner_prov());
                                    vip_question_detail_question_content.setText(detail.getQuestion());
                                    MyImageLoader.showImage(detail.getAnswerer_headimgurl(), vip_question_detail_answer_ima);
                                    vip_question_detail_answer_content.setText(detail.getAnswer());
                                    if (0 == detail.getIs_praised()) {
                                        //未点赞
                                        isPraised = false;
                                        Drawable d = VipQuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        vip_question_detail_zan.setCompoundDrawables(d, null, null, null);
                                    } else {
                                        isPraised = true;
                                        Drawable d = VipQuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima1);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        vip_question_detail_zan.setCompoundDrawables(d, null, null, null);
                                    }
                                    String state = detail.getState();
                                    MyLogUtils.e(TAG,"state"+state);
                                    if ("未回复".equals(state)) {
                                        vip_question_detail_evaluate.setVisibility(View.GONE);
                                        vip_question_detail_zan_layout.setVisibility(View.VISIBLE);
                                        vip_question_detail_comment_layout.setVisibility(View.VISIBLE);
                                    } else if("未评价".equals(state)){
                                        vip_question_detail_evaluate.setVisibility(View.VISIBLE);
                                        vip_question_detail_zan_layout.setVisibility(View.GONE);
                                        vip_question_detail_comment_layout.setVisibility(View.GONE);
                                    }else{
                                        vip_question_detail_evaluate.setVisibility(View.GONE);//评价按钮消失
                                        vip_question_detail_zan_layout.setVisibility(View.VISIBLE);//显示点赞
                                        vip_question_detail_comment_layout.setVisibility(View.VISIBLE);//显示评论量
                                    }
                                    vip_question_detail_zan.setText(detail.getPraise());
                                    zan = Integer.parseInt(detail.getPraise());
                                    vip_question_detail_comment.setText(detail.getComment());
                                }
                            }
                            break;
                        case ZAN:
                            try {
                                JSONObject jsonObject = new JSONObject((String) msg.obj);
                                String error_code = jsonObject.getString("err_code");
                                if ("0".equals(error_code)) {
                                    //赞
                                    zan ++;
                                    isPraised = true;
                                    if (!TextUtils.isEmpty(vip_question_detail_zan.getText().toString())) {
                                        vip_question_detail_zan.setText("" + (Integer.parseInt(vip_question_detail_zan.getText().toString()) + 1));
                                        Drawable d = VipQuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima1);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        vip_question_detail_zan.setCompoundDrawables(d, null, null, null);
                                    }
                                } else if ("1".equals(error_code)) {
                                    zan --;
                                    isPraised = false;
                                    //取消赞
                                    if (!TextUtils.isEmpty(vip_question_detail_zan.getText().toString())) {
                                        vip_question_detail_zan.setText("" + (Integer.parseInt(vip_question_detail_zan.getText().toString()) - 1));
                                        Drawable d = VipQuestionDetail.this.getResources().getDrawable(R.mipmap.college_topic_item_zan_ima);
                                        d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
                                        vip_question_detail_zan.setCompoundDrawables(d, null, null, null);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            break;
                        default:
                            break;
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialogId = getIntent().getStringExtra("dialogId");
        type = getIntent().getStringExtra("type");
        initView();
        getDataFromInterNet();
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("问题详情");
        vip_question_detail_questioner_ima.setOnClickListener(this);
        vip_question_detail_questioner_name.setOnClickListener(this);
        vip_question_detail_questioner_detail.setOnClickListener(this);
        vip_question_detail_zan_layout.setOnClickListener(this);
        vip_question_detail_comment_layout.setOnClickListener(this);
        vip_question_detail_evaluate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.common_back:
                setReturn(false);
                break;
            case R.id.vip_question_detail_evaluate:
                //跳到评价页面
                if (hasLoad()) {
                    intent.setClass(this, EvaluateQuestion.class);
                    intent.putExtra("dialogId", dialogId);
                    intent.putExtra("questionDetailBean", questionDetailBean);
                    startActivityForResult(intent, 0);
                } else {
                    this.login(this);
                }
                break;
            case R.id.vip_question_detail_zan_layout:
                PriseDialog();
                break;
            default:
                break;
        }
    }

    private void getDataFromInterNet() {
        HashMap<String, String> hashMap = new HashMap<>();
        if (!TextUtils.isEmpty(dialogId)) {
            if (!TextUtils.isEmpty(type)) {
                hashMap.put("type", type);
            }
            hashMap.put("dialogid", dialogId);
            new InterNetController(this, Constant.DIALOGDETAIL, handler, hashMap, GETDATA);
        }
    }

    private void PriseDialog() {
        HashMap<String, String> hashMap = new HashMap<>();
        if (!TextUtils.isEmpty(dialogId)) {
            hashMap.put("dialog_id", dialogId);
            new InterNetController(this, Constant.DIALOGPRISE, handler, hashMap, ZAN,false);
        }
    }

    @Override
    public void onBackPressed() {
        setReturn(false);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_CANCELED) {
            if (resultCode == 2) {
                setReturn(true);
            }
        }
    }

    private void setReturn(boolean hasComment){
        Intent i = new Intent();
        i.putExtra("zan",zan);
        i.putExtra("comment",hasComment);
        i.putExtra("isPraised",isPraised);
        setResult(2,i);
        VipQuestionDetail.this.finish();
    }
}
